package com.dehui.traders.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.TextAppearanceSpan;
import android.widget.TextView;

import com.dehui.traders.R;
import com.dehui.traders.bean.IndexInfo;
import com.dehui.traders.bean.RealtimeQuote;
import com.dehui.traders.widgets.MyHScrollView;

import java.util.List;

import me.goldze.mvvmhabit.utils.Utils;

public class OptionCodeAdapter extends TableAdapter<RealtimeQuote>{

    public  static final int theNew = 0;
    private static final int ZhangFu = 1;
    private static final int ZhangDie = 2;
    private static final int ChengJiaoLiang = 3;
    private static final int ChengJiaoE = 4;
    private static final int Open = 5;
    private static final int Close = 6;
    private static final int Height = 7;
    private static final int Low = 8;
    private static final int Ting = 9;
    private Fragment fragment;

    public OptionCodeAdapter(Context context, List mDatas, Fragment fragment) {
        super(context, mDatas);
        this.fragment = fragment;
    }

    @Override
    public void setFragment(MyHScrollView scrollview) {
        scrollview.setFragemnt(fragment);
    }

    @Override
    public void setItemText(int position, TextView tv,RealtimeQuote item) {
        switch (position){
            case theNew:
                tv.setText(Utils.doubleToString(item.price));
                if (item.price - item.openPrice > 0){
                    tv.setTextColor(Color.RED);
                }else if (item.price - item.openPrice < 0){
                    tv.setTextColor(Color.GREEN);
                }else {
                    tv.setTextColor(Color.WHITE);
                }
                break;
            case ZhangFu:
                float s = (item.price - item.closePrice) * 100 / (float) item.closePrice;
                tv.setText(Utils.doubleToString(s) + "%");
                if (item.price - item.openPrice > 0){
                    tv.setTextColor(Color.RED);
                }else if (item.price - item.openPrice < 0){
                    tv.setTextColor(Color.GREEN);
                }else {
                    tv.setTextColor(Color.WHITE);
                }
                break;
            case ZhangDie:
                tv.setText(Utils.doubleToString(item.price - item.closePrice));
                if (item.price - item.openPrice > 0){
                    tv.setTextColor(Color.RED);
                }else if (item.price - item.openPrice < 0){
                    tv.setTextColor(Color.GREEN);
                }else {
                    tv.setTextColor(Color.WHITE);
                }
                break;
            case ChengJiaoLiang:
                Double val = Double.valueOf(item.volume);
                tv.setText(Utils.doubleToString(val / 1000000) + "万");
                tv.setTextColor(Color.RED);
                break;
            case ChengJiaoE:
                tv.setText(Utils.doubleToString(item.volumnPrice / 10000));
                tv.setTextColor(Color.RED);
                break;
            case Open:
                tv.setText(Utils.doubleToString(item.openPrice));
                tv.setTextColor(Color.GREEN);
                break;
            case Close:
                tv.setText(Utils.doubleToString(item.closePrice));
                tv.setTextColor(Color.RED);
                break;
            case Height:
                tv.setText(Utils.doubleToString(item.highPrice));
                tv.setTextColor(Color.RED);
                break;
            case Low:
                tv.setText(Utils.doubleToString(item.lowPrice));
                tv.setTextColor(Color.RED);
                break;
            case Ting:
                tv.setText(item.isStop);
                break;
        }

    }

    @Override
    public SpannableString setTv0(RealtimeQuote item) {
        String title = item.name + "\n" + item.code;
        SpannableString styledText = new SpannableString(title);
        styledText.setSpan(new TextAppearanceSpan(mContext, R.style.style_title_0), 0, item.name.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        styledText.setSpan(new TextAppearanceSpan(mContext, R.style.style_title_1), item.name.length(), title.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return styledText;
    }


    @Override
    public int getL() {
        return 10;
    }
}
